package cn.jbolt.core.sensitiveword;

import cn.hutool.core.io.FileUtil;
import cn.jbolt.core.base.config.JBoltConfig;
import cn.jbolt.core.service.JBoltSensitiveWordService;
import com.jfinal.aop.Aop;
import com.jfinal.kit.StrKit;
import toolgood.words.WordsMatch;
import toolgood.words.WordsSearchResult;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 敏感词工具类
 */
public class JBoltSensitiveWordUtil {
    public static final JBoltSensitiveWordUtil me = new JBoltSensitiveWordUtil();
    private final static String FILEPATH = "sensitiveword/words.txt";
    private final static String DEFAULT_REPLACE_STR = "*";
    private WordsMatch searchLib;
    private int count;
    private JBoltSensitiveWordService service = Aop.get(JBoltSensitiveWordService.class);
    /**
     * 初始化敏感词词库加载
     */
    public void init() {
        System.out.println("开始初始化敏感词库");
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法初始化敏感词词库");
        }
        count= 0;
        searchLib = new WordsMatch();
        List<String> words = readAllWords();
        try {
            searchLib.SetKeywords(words);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("初始化敏感词库失败");
        }
    }

    /**
     * 敏感词检测 返回第一个
     * @param content
     * @return
     */
    public String findFirst(String content) {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库检测功能");
        }
        if (StrKit.isBlank(content)) {
            return null;
        }
        WordsSearchResult result = searchLib.FindFirst(content);
        return result == null ? null : result.MatchKeyword;
    }

    /**
     * 敏感词检测 返回所有
     * @param content
     * @return
     */
    public List<String> findAllWords(String content) {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库检测功能");
        }
        if (StrKit.isBlank(content)) {
            return null;
        }
        List<WordsSearchResult> results = searchLib.FindAll(content);
        if(results == null || results.size() == 0){
            return null;
        }
        return results.stream().map(r->r.MatchKeyword).collect(Collectors.toList());
    }

    /**
     * 敏感词检测 返回所有
     * @param content
     * @return
     */
    public List<WordsSearchResult> findAll(String content) {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库检测功能");
        }
        if (StrKit.isBlank(content)) {
            return null;
        }
        return searchLib.FindAll(content);
    }

    /**
     * 敏感词总数
     * @return
     */
    public int getCount() {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库getCount功能");
        }
        return count;
    }

    /**
     * 敏感词检测
     * @param content
     * @return
     */
    public boolean containsAny(String content) {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库检测功能");
        }
        if (StrKit.isBlank(content)) {
            return false;
        }
        return searchLib.ContainsAny(content);
    }

    /**
     * 敏感词 替换为 *
     * @param content
     * @return
     */
    public String replace(String content) {
        return replace(content,'*');
    }
    /**
     * 敏感词检测
     * @param content
     * @param replaceStr
     * @return
     */
    public String replace(String content,char replaceStr) {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库检测功能");
        }
        if (StrKit.isBlank(content)) {
            return null;
        }
        if(Character.isSpaceChar(replaceStr)){
            throw new RuntimeException("敏感词词库replaceStr 不能为空格");
        }
        return searchLib.Replace(content,replaceStr);
    }

    /**
     * reload重新加载词库
     */
    public void reload() {
        System.out.println("开始重新加载敏感词库");
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法使用敏感词词库重新加载功能");
        }
        List<String> words = readAllWords();
        try {
            searchLib.SetKeywords(words);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("重新加载敏感词库失败");
        }
    }

    /**
     * 从配置文件读取名敏感词
     * @return
     */
    private List<String> readAllWords() {
        if (!JBoltConfig.SENSITIVE_WORD_CHECK_ENABLE){
            throw new RuntimeException("平台配置参数sensitive_word_check_enable=false 无法读取敏感词词库数据");
        }

        List<String> words = service.getAllEnableWords();
        if(words==null || words.size()==0){
            words = FileUtil.readUtf8Lines(FILEPATH);
        }
        if(words!=null){
            count = words.size();
        }
        return words;
    }
}
